﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.OCR;

namespace AsposeOCR
{
    public class SimpleEngCaptcha
    {
        public string GetTxtFromFile(string filename)
        {
            try
            {
                OcrEngine ocrEngine = new OcrEngine
                {
                    Image = ImageStream.FromFile(filename)
                };
                if (ocrEngine.Process())
                {
                    return ocrEngine.Text.ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return e.Message;
            }
        }

        public string GetTxt(string content, ImageStreamFormat type = ImageStreamFormat.Png)
        {
            byte[] byteContent = Convert.FromBase64String(content);
            return GetTxt(byteContent, type);
        }

        public string GetTxt(byte[] content, ImageStreamFormat type = ImageStreamFormat.Png)
        {
            try
            {
                OcrEngine ocrEngine = new OcrEngine();
                MemoryStream ms = new MemoryStream(content)
                {
                    Position = 0
                };
                ocrEngine.Image = ImageStream.FromStream(ms, type);
                if (ocrEngine.Process())
                {
                    return ocrEngine.Text.ToString();
                }
                return "";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return e.Message;
            }
        }
    }
}
